<?php
/*
 * Created on 03.01.2006
 *
 */
 
class dbaShop
{

    var $dba;

    function dbaShop (&$dba)
    {
        $this->dba = & $dba;
    }
    
    function getShopStats()
    {
        $stats = array();
        $sth = $this->dba->query("select Year(FROM_UNIXTIME(timestamp)) as year, Month(FROM_UNIXTIME(timestamp)) as month, sum(total_amount) sum " .
                "from ".$this->dba->tableforwarder."_shopOrder group by Year(FROM_UNIXTIME(timestamp)), Month(FROM_UNIXTIME(timestamp));");
        $stats['sum_per_month'] = $sth->fetch_rows_array();
        $sth = $this->dba->query("select Year(FROM_UNIXTIME(timestamp)) as year, sum(total_amount) sum " .
                "from ".$this->dba->tableforwarder."_shopOrder group by Year(FROM_UNIXTIME(timestamp));");
        $stats['sum_per_year'] = $sth->fetch_rows_array();
        $sth = $this->dba->query("select sum(total_amount) sum from ".$this->dba->tableforwarder."_shopOrder;");
        $stats['sum'] = $sth->fetch_rows_array();
    }
    
    function getPubAll ($cats, $lang, $additionalCond = '')
    {
        $notnull = array();
        foreach ($cats as $c)
            if ($c)
                $notnull[] = $c;
        if ($additionalCond)
                $cond =  "and (".$additionalCond.")";
        if (count($notnull) > 0)
        {            
            $cat = "R3.cat_id = ".join(" or R3.cat_id = ", $notnull);
            $sth = $this->dba->query("select distinct R1.*, " .
                "R2.satz As mwst_satz from ".$this->dba->tableforwarder."_cmsPubType_shopproduct R1, ".
                $this->dba->tableforwarder."_shopmwst R2, ".$this->dba->tableforwarder.
                "_cmsElementObjCat R3 ".
                "where R1.mwst_foreignkey = R2.id and R3.element_id = R1.element_id and (".$cat.") and lang='$lang' $cond group by id, element_id, current, lang, meta_author, meta_built, name_text, orderno_text, price_ddecimal, mwst_foreignkey, short_text, short_autobreak, long_text, long_autobreak, list_href, list_alttext, list_map, detail_href, detail_alttext, detail_map, packet_checkbox, weight_ddecimal having count(*) = ".count($notnull)." order by orderno_text",true);
        }
        else
        {
            $sth = $this->dba->query("select distinct R1.*, " .
                "R2.satz As mwst_satz from ".$this->dba->tableforwarder."_cmsPubType_shopproduct R1, ".
                $this->dba->tableforwarder."_shopmwst R2 where R1.mwst_foreignkey = R2.id and lang='$lang' $cond order by orderno_text");
        }
        if ($sth && $this->dba->num_rows())
        {
            $rows = $sth->fetch_rows_array();
            $sth->free();
            return $rows;
        }
    }

    function getPub ($id)
    {
        $sth = $this->dba->query("select distinct R1.*, " .
                "R2.satz As mwst_satz from ".$this->dba->tableforwarder."_cmsPubType_shopproduct R1, ".
                $this->dba->tableforwarder."_shopmwst R2".
                " where R1.mwst_foreignkey = R2.id and R1.id = $id  order by orderno_text");
        if ($sth)
        {
            $row = $sth->fetch_row_array();
            $sth->free();
            return $row;
        }
    }

}


?>
